package com.seqb.utils;

import com.seqb.common.Constant;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.WordUtils;

/**
 * @author lilinshen
 * @title 数据库工具类
 * @description 请填写相关描述
 * @date 2019/11/11
 */
public class DatabaseUtils {

    /**
     * 列名转换成Java属性名
     */
    public static String columnToJava(String columnName) {
        return WordUtils.capitalizeFully(columnName, new char[]{'_'}).replace("_", "");
    }

    /**
     * 表名转换成Java类名
     */
    public static String tableToJava(String tableName, String tablePrefix, String autoRemovePre) {
        if (Constant.AUTO_REOMVE_PRE.equals(autoRemovePre)) {
            tableName = tableName.substring(tableName.indexOf("_") + 1);
        }
        if (StringUtils.isNotBlank(tablePrefix)) {
            tableName = tableName.replace(tablePrefix, "");
        }

        return columnToJava(tableName);
    }

    /**
     * Java类名转换成表名
     */
    public static String javaToTable(String tableDoName) {
        tableDoName = tableDoName.replaceAll("DO", "");
        StringBuffer tableName = null;
        for (char c : tableDoName.toCharArray()) {
            if (Character.isUpperCase(c)) {
                if (tableName == null) {
                    tableName = new StringBuffer().append("t_");
                } else {
                    tableName.append("_");
                }
                tableName.append(Character.toLowerCase(c));
            } else {
                tableName.append(c);
            }
        }
        return tableName.toString();
    }


}
